package com.vecspace.data.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.vecspace.utils.DateTimeUtil;

import comvecspace.data.model.sql.TDataType;
import comvecspace.data.model.sql.TSqlOperator;


/**对QueryField进行的封装
 * 
 * @version: 1.0
 * @author: wanghua
 */
public class FilterColumns {
	private List<FilterColumn> queryFields = new ArrayList<>();
	
	public static FilterColumns newInstance() {
		return new FilterColumns();
	}
	
	public FilterColumns() {
		
	}
	
	public FilterColumns(List<FilterColumn> queryFields) {
		if(queryFields != null) {
			this.queryFields.addAll(queryFields);
		}
	}
	
	/**字符串类型的相等查询条件
	 * 
	 * @param columnName
	 * @param queryValue 
	 * @author: wanghua
	 */
	public void addStringEqualQueryField(String columnName, String queryValue) {
		FilterColumn queryField = new FilterColumn(columnName, TDataType.ftString, TSqlOperator.opEqual, queryValue);
		queryFields.add(queryField);
	}
	
	/**数字类型的相等查询条件
	 * 
	 * @param columnName
	 * @param queryValue 
	 * @author: wanghua
	 */
	public void addNumberEqualQueryField(String columnName, int queryValue) {
		FilterColumn queryField = new FilterColumn(columnName, TDataType.ftNumber, TSqlOperator.opEqual, String.valueOf(queryValue));
		queryFields.add(queryField);
	}
	
	/**数字类型的相等查询条件
	 * 
	 * @param columnName
	 * @param queryValue 
	 * @author: wanghua
	 */
	public void addNumberEqualQueryField(String columnName, Long queryValue) {
		FilterColumn queryField = new FilterColumn(columnName, TDataType.ftNumber, TSqlOperator.opEqual, String.valueOf(queryValue));
		queryFields.add(queryField);
	}
	
	/**日期类型的 Between 查询条件
	 * 
	 * @param columnName
	 * @param queryValue 
	 * @author: wanghua
	 */
	public void addDateBetweenQueryField(String columnName, Date startDate, Date endDate) {
		String startDateStr = DateTimeUtil.formatDate(startDate, "yyyy-MM-dd HH:mm:ss");
		String endDateStr = DateTimeUtil.formatDate(endDate, "yyyy-MM-dd HH:mm:ss");
		String queryValue = startDateStr + "|" + endDateStr;
		FilterColumn queryField = new FilterColumn(columnName, TDataType.ftDate, TSqlOperator.opBetween, queryValue);
		queryFields.add(queryField);
	}
	
	public void addQueryField(String columnName, TDataType dataType, TSqlOperator operator, String queryValue) {
		FilterColumn queryField = new FilterColumn(columnName, dataType, operator, queryValue);
		queryFields.add(queryField);
	}
	
	/**移除指定
	 * 
	 * @param columnName
	 * @return 
	 * @author wanghua
	 */
	public FilterColumn removeQueryField(String columnName) {
		for(int i=queryFields.size()-1; i>=0 ;--i) {
			FilterColumn queryField = queryFields.get(i);
			if(columnName.equalsIgnoreCase(queryField.getColumnName())) {
				queryFields.remove(i);
				return queryField;
			}
		}
		return null;
	}

	/**
	 * 
	 * @return 返回查询条件集合（只读） 
	 * @author: wanghua
	 */
	public List<FilterColumn> getQueryFields() {
		return Collections.unmodifiableList(queryFields);
	}

	public void setQueryFields(List<FilterColumn> queryFields) {
		this.queryFields = queryFields;
	}
}
